//
//  HomepageCollectionTableViewCell.m
//  CenturyInterconnection
//
//  Created by Tian Wei You on 16/4/8.
//  Copyright © 2016年 Shanghai DuRui Information Technology Company. All rights reserved.
//

#import "HomepageCollectionTableViewCell.h"
#import "HomepageCollectionViewCell.h"

#define HomepageCell  @"HomepageCollectionViewCell"

@implementation HomepageCollectionTableViewCell

-(void)DelegateCollectionToReload{
    [self.collectionView reloadData];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UICollectionViewFlowLayout*flowLayout=[[UICollectionViewFlowLayout alloc]init];
        
        flowLayout.minimumLineSpacing=10;
        flowLayout.minimumInteritemSpacing=0;
        flowLayout.itemSize=CGSizeMake(ACTUAL_WIDTH(175), ACTUAL_HEIGHT(230));
        flowLayout.sectionInset=UIEdgeInsetsMake(ACTUAL_HEIGHT(0), ACTUAL_WIDTH(8), ACTUAL_HEIGHT(0), ACTUAL_WIDTH(8));
        
        UICollectionView*collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, ACTUAL_HEIGHT(0), KScreenWidth,ACTUAL_HEIGHT(700)) collectionViewLayout:flowLayout];
        collectionView.showsHorizontalScrollIndicator=NO;
        collectionView.showsVerticalScrollIndicator=NO;
        collectionView.delegate=self;
        collectionView.dataSource=self;
        collectionView.scrollEnabled=NO;
        [collectionView registerNib:[UINib nibWithNibName:HomepageCell bundle:nil] forCellWithReuseIdentifier:HomepageCell];
        collectionView.backgroundColor=[UIColor whiteColor];
        self.collectionView=collectionView;
        
        [self.contentView addSubview:collectionView];
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make)
        {
            make.top.mas_equalTo(self.top).offset(0);
            make.left.mas_equalTo(self.left).offset(0);
            make.right.mas_equalTo(self.right).offset(0);
            make.bottom.mas_equalTo(self.mas_bottom).offset(0);
        }];

    }
    return self;
}

- (void)awakeFromNib {
   }

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _datas.count;
    
}

-(void)getValue:(NSArray *)array{
    self.datas=array;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomepageCollectionViewCell*cell=[collectionView dequeueReusableCellWithReuseIdentifier:HomepageCell forIndexPath:indexPath];
//    cell.backgroundColor=[UIColor redColor];
    UIImageView *imageView=[cell viewWithTag:1];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.datas[indexPath.item][@"products_image"]] placeholderImage:nil];
    
    UILabel *label1=[cell viewWithTag:2];
    label1.text=self.datas[indexPath.item][@"products_name"];
    
    UILabel *label2=[cell viewWithTag:3];
    label2.text=self.datas[indexPath.item][@"products_price"];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    //NSLog(@"%@",self.datas[indexPath.item][@"products_id"]);
    if ([self.delegate respondsToSelector:@selector(cellOfcollectionselected:)]) {
        [self.delegate cellOfcollectionselected:self.datas[indexPath.item][@"products_id"]];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
